package com.ruoyi.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.cm.domain.bo.CmBsContractInfoMilestoneBo;
import com.ruoyi.cm.domain.vo.CmBsContractInfoMilestoneVo;
import com.ruoyi.cm.domain.CmBsContractInfoMilestone;
import com.ruoyi.cm.mapper.CmBsContractInfoMilestoneMapper;
import com.ruoyi.cm.service.ICmBsContractInfoMilestoneService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 合同信息里程碑Service业务层处理
 *
 * @author zyb
 * @date 2024-09-27
 */
@RequiredArgsConstructor
@Service
public class CmBsContractInfoMilestoneServiceImpl implements ICmBsContractInfoMilestoneService {

    private final CmBsContractInfoMilestoneMapper baseMapper;

    /**
     * 查询合同信息里程碑
     */
    @Override
    public CmBsContractInfoMilestoneVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询合同信息里程碑列表
     */
    @Override
    public TableDataInfo<CmBsContractInfoMilestoneVo> queryPageList(CmBsContractInfoMilestoneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmBsContractInfoMilestone> lqw = buildQueryWrapper(bo);
        Page<CmBsContractInfoMilestoneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询合同信息里程碑列表
     */
    @Override
    public List<CmBsContractInfoMilestoneVo> queryList(CmBsContractInfoMilestoneBo bo) {
        LambdaQueryWrapper<CmBsContractInfoMilestone> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmBsContractInfoMilestone> buildQueryWrapper(CmBsContractInfoMilestoneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmBsContractInfoMilestone> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getContractId() != null, CmBsContractInfoMilestone::getContractId, bo.getContractId());
        lqw.eq(bo.getBeginDate() != null, CmBsContractInfoMilestone::getBeginDate, bo.getBeginDate());
        lqw.eq(bo.getEndDate() != null, CmBsContractInfoMilestone::getEndDate, bo.getEndDate());
        lqw.eq(bo.getKgyfkbl() != null, CmBsContractInfoMilestone::getKgyfkbl, bo.getKgyfkbl());
        lqw.eq(StringUtils.isNotBlank(bo.getZftk()), CmBsContractInfoMilestone::getZftk, bo.getZftk());
        lqw.eq(StringUtils.isNotBlank(bo.getProcInsId()), CmBsContractInfoMilestone::getProcInsId, bo.getProcInsId());
        lqw.eq(StringUtils.isNotBlank(bo.getChgProcInsId()), CmBsContractInfoMilestone::getChgProcInsId, bo.getChgProcInsId());
        return lqw;
    }

    /**
     * 新增合同信息里程碑
     */
    @Override
    public Boolean insertByBo(CmBsContractInfoMilestoneBo bo) {
        CmBsContractInfoMilestone add = BeanUtil.toBean(bo, CmBsContractInfoMilestone.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改合同信息里程碑
     */
    @Override
    public Boolean updateByBo(CmBsContractInfoMilestoneBo bo) {
        CmBsContractInfoMilestone update = BeanUtil.toBean(bo, CmBsContractInfoMilestone.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmBsContractInfoMilestone entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除合同信息里程碑
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
